package websimulator;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import readwrite.Connector;

/**
 * Servlet implementation class NewHouse
 */
@WebServlet("/NewHouse")
public class NewHouse extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connector connector;
	private int people, directive, buildingType;
	private double area, wallHeight;
	private int userId = 1;
       
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public NewHouse() throws Exception {
        super();
        connector = new Connector();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Makes a new house that is added to the modul.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		try {
			people = Integer.parseInt(request.getParameter("people"));
			area = Double.parseDouble(request.getParameter("area"));
			directive = Integer.parseInt(request.getParameter("dID"));
			wallHeight = Double.parseDouble(request.getParameter("wallHeight"));
			buildingType = Integer.parseInt(request.getParameter("tID"));
			
			connector.AddHouse(people, area, directive, wallHeight, userId, buildingType);
			int houseID = connector.GetLastHouseID();
			int modulID = Integer.parseInt(request.getParameter("modulID"));
			
			
			if(houseID != 0 || modulID != 0)
			connector.AddModulHouse(houseID, modulID);
			
			String answer = "<div id=\"house\"><h4>du la til ett hus.</h4></div>" ;
			
			people = 0;
			area = 0;
			directive = 0;
			wallHeight = 0;
			buildingType = 0;
			houseID = 0;
			modulID = 0;
			response.setContentType("text/plain");
			response.getWriter().write(answer);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
